import { createRouter, createWebHistory, createMemoryHistory, START_LOCATION } from 'vue-router'
import { constantRoutes } from './routes'
import defaultRoutes from './defaultRoutes'

export const routes = constantRoutes
// export const routes = constantRoutes.concat(defaultRoutes);

// 返回上一级页面的浏览位置
const scrollBehavior = (to, from, savedPosition) => {
  // scroll to id `#app` + 200px, and scroll smoothly (when supported by the browser)
  if (savedPosition) {
    return savedPosition
  } else {
    return {
      el: '#app',
      top: 0,
      left: 0,
      behavior: 'smooth',
    }
  }
}

const isServerClient = false
const history = isServerClient ? createMemoryHistory() : createWebHistory()
const router = createRouter({
  routes,
  history,
  scrollBehavior,
})

export function setupRouter(app) {
  app.use(router)
}


export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
